﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
	public class EIRepository : IEIRepository
	{
		private readonly MyDbContext myDbContext;

		public EIRepository(MyDbContext myDbContext)
		{
			this.myDbContext = myDbContext;
		}
		public async Task<bool> AddEIAsync(EI ei)
		{
			ei.CheckStatus = 1;
			myDbContext.EI.Add(ei);
			int result = await myDbContext.SaveChangesAsync();
			return result > 0;
		}

		public EIFenYe DateEIAsync(int page, int pageSize, int zt)
		{
			var query = myDbContext.EI.AsQueryable();

			if (!string.IsNullOrEmpty(zt.ToString()))
			{
				query = query.Where(s => s.CheckStatus == zt);
			}
			int totalRecords = query.Count();
			var EI = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new EIFenYe
			{
				totalRecords = totalRecords,
				ei = EI
			};
		}

		public async Task<bool> DeleteEIAsync(int id)
		{
			EI ei = new EI()
			{
				EinId = id
			};
			myDbContext.EI.Remove(ei);
			int changes = await myDbContext.SaveChangesAsync();
			return changes > 0;
		}

		public async Task<EI> GetEIByNameAsync(int id)
		{
			return await myDbContext.EI.Where(e => e.ResumeId == id).FirstOrDefaultAsync();
		}

		public async Task<bool> UpdateEIAsync(EI ei)
		{
			EI eis = await myDbContext.EI.Where(e => e.EinId == ei.EinId).FirstOrDefaultAsync();
			eis.Checker = ei.Checker;
			eis.CheckTime = ei.CheckTime;
			eis.CheckComment = ei.CheckComment;
			eis.CheckStatus = ei.CheckStatus;
			myDbContext.EI.Update(eis);
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public async Task<bool> UpdateEISAsync(EI ei)
		{
			myDbContext.EI.Update(ei);
			return await myDbContext.SaveChangesAsync() > 0;
		}
	}
}
